package com.blog.web.controller.blog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.blog.system.domain.blog.BlogTags;
import com.blog.system.domain.blog.dto.BlogArticleDTO;
import com.blog.system.domain.blog.vo.BlogArticleVO;
import com.blog.system.service.blog.IBlogTagsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blog.common.annotation.Log;
import com.blog.common.core.controller.BaseController;
import com.blog.common.core.domain.AjaxResult;
import com.blog.common.enums.BusinessType;
import com.blog.system.domain.blog.BlogArticle;
import com.blog.system.service.blog.IBlogArticleService;
import com.blog.common.utils.poi.ExcelUtil;
import com.blog.common.core.page.TableDataInfo;

/**
 * 博客文章Controller
 * 
 * @author shibalover
 * @date 2024-03-31
 */
@RestController
@RequestMapping("/blog/article")
public class BlogArticleController extends BaseController
{
    @Autowired
    private IBlogArticleService blogArticleService;

    @Autowired
    private IBlogTagsService blogTagsService;

    /**
     * 查询博客文章列表
     */
    @PreAuthorize("@ss.hasPermi('blog:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogArticleVO blogArticle)
    {
        startPage();
        List<BlogArticleVO> list = blogArticleService.selectBlogArticleList(blogArticle);
        return getDataTable(list);
    }

    /**
     * 导出博客文章列表
     */
    @PreAuthorize("@ss.hasPermi('blog:article:export')")
    @Log(title = "博客文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogArticleVO blogArticle)
    {
        List<BlogArticleVO> list = blogArticleService.selectBlogArticleList(blogArticle);
        ExcelUtil<BlogArticleVO> util = new ExcelUtil<>(BlogArticleVO.class);
        util.exportExcel(response, list, "博客文章数据");
    }

    /**
     * 获取博客文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return success(blogArticleService.selectBlogArticleByArticleId(articleId));
    }

    /**
     * 新增博客文章
     */
    @PreAuthorize("@ss.hasPermi('blog:article:add')")
    @Log(title = "博客文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogArticle blogArticle)
    {
        return toAjax(blogArticleService.insertBlogArticle(blogArticle));
    }

    /**
     * 修改博客文章
     */
    @PreAuthorize("@ss.hasPermi('blog:article:edit')")
    @Log(title = "博客文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogArticleDTO blogArticle)
    {
        //处理标签
        if (Objects.nonNull(blogArticle.getNewTags()) && blogArticle.getNewTags().size() > 0) {
            //插入新的标签数据
            List<BlogTags> list = new ArrayList<>();
            for (String newTag : blogArticle.getNewTags()) {
                list.add(new BlogTags(newTag));
            }
            blogTagsService.insertBlogTagsBatch(list);
            //获取新标签的id
            list.forEach(blogTags -> {
                blogArticle.getOldTags().add(blogTags.getTagsId());
            });
            //存入新的标签id
            blogArticle.setTags(JSONArray.toJSONString(blogArticle.getOldTags()));
        }
        return toAjax(blogArticleService.updateBlogArticle(blogArticle));
    }

    /**
     * 删除博客文章
     */
    @PreAuthorize("@ss.hasPermi('blog:article:remove')")
    @Log(title = "博客文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(blogArticleService.deleteBlogArticleByArticleIds(articleIds));
    }
}
